package com.jlz.actions.register;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Standard;
import org.springframework.beans.factory.annotation.Autowired;

import com.jlz.beans.user.LinkBean;
import com.jlz.beans.user.UserBean;
import com.jlz.managers.user.Links;
import com.jlz.managers.user.Users;
import com.jlz.service.email.EmailService;
import com.julewa.Client;
import com.julewa.utils.Patterns;

@Standard
public class RenoticeAction {
	@Autowired Client client;
	@Autowired Users users;
	@Autowired Links links;
	@Autowired EmailService emailsrv;
	private String email;
	
	public String execute(){
		email = StringUtils.trimToNull(email);
		if(null == email){
			return "renotice";
		}
		if(!Patterns.EMAIL.match(email)){
			client.putError("邮箱格式不对哟");
			return "renotice";
		}
		LinkBean link = links.find(email);
		if(link == null){
			client.putError("您还没注册呢~");
			return "renotice";
		}
		//
		switch(Links.State.of(link.getState())){
		case USING:{
			client.putError("您的账号已经激活,请在上面登陆");
			return "renotice";
		}
		case NEW:{
			UserBean user = users.get(link.getUserId());
			emailsrv.sendRegisterNotice(email, user.getName(),
					links.encode(link));
			return "notice";
		}
		default:
			client.putError("账号已激活");
			return "notice";
		}
	}
	
	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}
	
	public String getUsercode(){
		return email;
	}
 
}
